﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Collections;

namespace QuanLyVatLieuXayDung
{
    public partial class FormLapPhieuNhap : Form
    {
        private bool mousePressed = false;
        private Point diff = new Point(0, 0);
        public FormLapPhieuNhap()
        {
            BitmapRegion.BitmapRegion.CreateControlRegion(this, Properties.Resources.PhieuNhapHang);
            InitializeComponent();
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
        }

        private void FormLapPhieuNhap_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mousePressed = true;

                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                diff.X = p.X - DesktopLocation.X;
                diff.Y = p.Y - DesktopLocation.Y;
            }
        }

        private void FormLapPhieuNhap_MouseMove(object sender, MouseEventArgs e)
        {
            if (mousePressed && (e.Button & MouseButtons.Left) != 0)
            {
                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                p.X -= diff.X;
                p.Y -= diff.Y;
                DesktopLocation = p;
            } 
        }
        public ArrayList listCTPN = new ArrayList();
        private void FormLapPhieuNhap_MouseUp(object sender, MouseEventArgs e)
        {
            mousePressed = false;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        LayerDTO.XuLyChuoiKetNoiDTO xuLy = new LayerDTO.XuLyChuoiKetNoiDTO();

        int row = 0;
        int colum = 0;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                row = e.RowIndex;
                colum = e.ColumnIndex;
                FormTimMatHang frm = new FormTimMatHang();
                frm.sender = new FormTimMatHang.SEND(GetString);
                frm.ShowDialog();
            }
        }
        public void GetString(string s,string tenDonVi)
        {
            dataGridView1.Rows[row].Cells[colum].Value = s;
            dataGridView1.Rows[row].Cells[4].Value = tenDonVi;
        }
        private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {

            }
            if (e.ColumnIndex == 2)
            {
                try
                {
                    float.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                    if (dataGridView1.Rows[e.RowIndex].Cells[3].Value != null)
                    {
                       float tien =(float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()) * float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()));
                       dataGridView1.Rows[e.RowIndex].Cells[5].Value = String.Format("{0:0,0}", tien); 
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Đơn Giá Phải Nhập Số","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                }
            }
            if (e.ColumnIndex == 3)
            {
                try
                {
                    int.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                    if (dataGridView1.Rows[e.RowIndex].Cells[2].Value != null)
                    {
                        float tien = (float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()) * float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()));
                        dataGridView1.Rows[e.RowIndex].Cells[5].Value = String.Format("{0:0,0}", tien); 
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Số Lượng Phải Nhập Số", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                }
            }
        }

        private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            
        }

        private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows[e.RowIndex].Cells[1].Value == null || dataGridView1.Rows[e.RowIndex].Cells[2].Value == null || dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() == null || dataGridView1.Rows[e.RowIndex].Cells[4].Value == null || dataGridView1.Rows[e.RowIndex].Cells[5].Value == null)
            {
                MessageBox.Show("Bạn Chưa Nhập Đầy Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                themDong = false;
            }
            else
            {
                themDong = true;
            }
        }
        bool themDong = true;

        private void button1_Click(object sender, EventArgs e)
        {
            if (themDong == true)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = dataGridView1.Rows.Count;
                themDong = false;
            }
            else
            {
                MessageBox.Show("Bạn Chưa Nhập Đầy Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
            }
            catch (Exception ex)
            { }
        }
    }
}
